﻿define(["admin/app", "service/admin/topicService", "service/admin/productService", "ui-bootstrap"], function (app) {
    "use strict"

    app.controller("topicController", function ($scope, $state, $stateParams, $uibModal, utils, w5cValidator, baseService, topicService, productService) {
        $scope.list = {};
        $scope.model = {};
        //专题对象
        $scope.topic = {
            banner: "1",
            create_time: "",
            id: 0,
            remark: "",
            show_images: "1",
            show_type: 0,
            title: "",
            topic_groups: []
        };
        $scope.topic_group = {
            id: 0,
            name: "",
            show_images: "1",
            topic_products:[]
        };
        $scope.topic_product = {
            id: 0,
            product: {},
            product_id: 0,
            sort: 0
        };
        $scope.products = [];

        $scope.query = {
            page: 1, size: 20, 
        };

        $scope.product_query = {
            page: 1, size: 5,
        };
        $.extend($scope.query, $stateParams);
        //商品弹窗选择
        var data = "";
        var index = "";
        $scope.openModal = function (n) {
            index = n;
            var modalInstance = $uibModal.open({
                templateUrl: 'modal.html',//script标签中定义的id
                controller: 'modalCtrl',//modal对应的Controller
                windowClass: 'large-Modal',
                resolve: {
                    data: function () {//data作为modal的controller传入的参数
                        //获取商品列表
                        return data = productService.gets($scope.product_query).success(function (response) {
                            return response.data;//用于传递数据
                        });;
                    },
                    index: function () {
                        //对应分组索引
                        return index;
                    },
                    parentScope: function () {
                        return $scope;
                    }
                }
            })
        };
        //模态框对应的Controller
        app.controller('modalCtrl', function ($scope, $uibModalInstance, parentScope, data, index) {
            $scope.data = data.data.data;
            var count = [];
            //在这里处理要进行的操作
            $scope.ok = function () {
                count.push(index);
                var ids = $("input[type='checkbox']:checked");
                parentScope.topic_group.products = [];
                angular.forEach($scope.data.rows,function(item,key){
                    angular.forEach(ids, function (value, key) { 
                        if (value.value == item.id.toString()) {
                            //$scope.product.push(item);
                            let products = {};
                            //let _products = {};
                            //添加分组商品
                            products.product = item;
                            products.product_id = item.id;
                            //parentScope.topic_product.product = item;
                            //parentScope.topic_product.product_id = item.id;
                            //products = parentScope.topic_product;
                            //if (parentScope.topic.topic_groups[index].topic_products != undefined) {
                            //    for (var i = 0; i < parentScope.topic.topic_groups[index].topic_products.length; i++) {
                            //        if (parentScope.topic_group.topic_products.length > 0) {
                            //            if (parentScope.topic_group.topic_products[i].product_id == item.id) {
                            //                utils.notify("商品不能重复选择", "warning");
                            //                return;
                            //            }
                            //        }
                            //    }
                            //}
                       
                            parentScope.topic_group.topic_products.push(products);
                            //parentScope.topic_group.topic_products.push(_products);
                            if (parentScope.model.topic_groups[index].topic_products == undefined || parentScope.model.topic_groups[index].topic_products == null)
                                parentScope.model.topic_groups[index].topic_products = [];
                            parentScope.model.topic_groups[index].topic_products.push(products);
                        }
                    });
                });
                //添加分组
                //parentScope.topic_group.name = parentScope.model.topic_groups[index].name;
                //parentScope.topic_group.show_images = parentScope.model.topic_groups[index].show_images;
                //parentScope.topic_group.show_images = "1";
                //parentScope.topic_group.file = parentScope.model.topic_groups[index].file;
                for (var i = 0; i < count.length; i++) {
                    var num = 1;
                    if (count[i] == index)
                        num += 1;
                    if (num == 1)
                      parentScope.topic.topic_groups.push(parentScope.topic_group);
                }
                $uibModalInstance.close();
            };
            $scope.search_page = function (page) {
                parentScope.product_query.page = page;
                productService.gets(parentScope.product_query).success(function (response) {
                    $scope.data = response.data;//用于传递数据
                });
            };
            $scope.cancel = function () {
                $uibModalInstance.dismiss('cancel');
            };
        });
        var methods = {
            search: function () {
                topicService.gets($scope.query).success(function (response) {
                    $scope.list = response.data;
                });
            },
            get_product: function () {
                productService.gets($scope.query).success(function (response) {
                    $scope.product = response.data;
                    return response.data;//用于传递数据
                });
            },
            edit: function () {
                if ($stateParams.id > 0) {
                    topicService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                        $scope.topic = response.data;
                        $scope.model.show_type = response.data.show_type.toString();
                    });
                    $scope.breadcrumb = "编辑";
                } else {
                    $scope.breadcrumb = "新增";
                }
                w5cValidator.setRules({
                    title: {required: '请填写专题标题' }
                });
            },
            get_show_type: function (type) {
                var show_type = new Array("幻灯片","专区","专题精选","专题","带你看世界");
                return show_type[type - 1];
            },
            goBack: function () {
                history.back();
            },
            remove: function (id) {
                var modal = utils.confirm({ msg: "确定要删除吗？", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    topicService.delete(id).success(function () {
                        methods.search();
                    });
                });
            },
            delete: function (n,m) {
                //n商品索引 m分组索引
                $scope.model.topic_groups[m].topic_products.splice(n, 1);
                $scope.topic.topic_groups[m].topic_products.splice(n, 1);
            },
            save: function () {
                $scope.topic.title = $scope.model.title;
                //$scope.topic.show_images = $scope.model.show_images;
                $scope.topic.file = $scope.model.file;
                $scope.topic.file1 = $scope.model.file1;

                for (var i = 0; i < $scope.topic.topic_groups.length; i++) {
                    $scope.topic.topic_groups[i].name = $scope.model.topic_groups[i].name;
                    $scope.topic.topic_groups[i].file = $scope.model.topic_groups[i].file;
                }
                var model = $scope.topic;
                model.show_type = $scope.model.show_type;
                if ($scope.model.id > 0)
                    model.id = $scope.model.id;
                $scope.psdisabled = true;
                topicService.save(model).success(function (response) {
                    // utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    $scope.psdisabled = false;
                    if (response.result_code == 1000) {
                        methods.goBack();
                    }
                });
            },
            detail: function () {
                if ($stateParams.id > 0) {
                    var params = { id: $stateParams.id };
                    topicService.get(params).success(function (response) {
                        $scope.model = response.data;
                    });
                }
            },
            createGroup: function () {
                let topic_group = [];
                if ($scope.model == undefined || $scope.model == null) {
                    $scope.model = {};
                }
                if ($scope.model.topic_groups == undefined || $scope.model.topic_groups == null)
                    $scope.model.topic_groups = [];

                if ($stateParams.id > 0) {
                    $scope.model.topic_groups.push(topic_group);
                } else {
                    $scope.model.topic_groups.push(topic_group);
                    $scope.topic.topic_groups.push(topic_group);
                }
       
            },
            deleteGroup: function (n) {
                $scope.model.topic_groups.splice(n, 1);
                $scope.topic.topic_groups.splice(n, 1);
            }
        };
        angular.extend($scope, methods);
        if ($state.includes('app.product.topic_detail')) {
            methods.edit();
        } else {
            methods.search();
        }
    });
});